package com.czl.module_storehouse.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.adapter.tree.ListTree;
import com.czl.module_storehouse.adapter.tree.ListTreeAdapter;

import java.util.List;

/**
 * @author by xubin,
 * @date on 2022/2/24.
 * PS: Not easy to write code, please indicate.
 */

public class InventoryLocationAdapter extends ListTreeAdapter<InventoryLocationAdapter.BaseViewHolder> {

    private OnItemClickListener mOnItemClickListener;

    public InventoryLocationAdapter(ListTree tree) {
        super(tree);
    }

    public <T> void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void addAll(List<LocatListBean> list) {
        for (LocatListBean bean : list) {
            //创建组们，是root node，所有parent为null
//            bean.setLevel(0);
            ListTree.TreeNode groupNode = tree.addNode(null, bean, R.layout.item_category_parent);
            groupNode.setExpand(false);
            List<LocatListBean> children = bean.getChildList();
            if (children == null) {
                continue;
            }
            for (LocatListBean childrenBean : children) {
                List<LocatListBean> childrenXs = childrenBean.getChildList();
//                childrenBean.setLevel(1);
                ListTree.TreeNode treeNode1 = tree.addNode(groupNode, childrenBean, R.layout.item_category_children);
                treeNode1.setExpand(false);
                if (childrenXs == null) {
                    continue;
                }
                for (LocatListBean childrenBeanX : childrenXs) {
                    //第三层
//                    childrenBean.setLevel(2);
                    ListTree.TreeNode n = tree.addNode(treeNode1, childrenBeanX, R.layout.item_category_children);
                    n.setExpand(false);
                }
            }
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    protected BaseViewHolder onCreateNodeView(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        //创建不同的行View
        if (viewType == R.layout.item_category_parent) {
            //注意！此处有一个不同！最后一个参数必须传true！
            View view = inflater.inflate(viewType, parent, true);
            //用不同的ViewHolder包装
            return new InventoryLocationAdapter.GroupViewHolder(view);
        } else if (viewType == R.layout.item_category_children) {
            //注意！此处有一个不同！最后一个参数必须传true！
            View view = inflater.inflate(viewType, parent, true);
            //用不同的ViewHolder包装
            return new InventoryLocationAdapter.ContactViewHolder(view);
        } else {
            return null;
        }
    }

    @Override
    protected void onBindNodeViewHolder(BaseViewHolder viewHolder, int position) {
        ListTree.TreeNode node = tree.getNodeByPlaneIndex(position);
        if (node.getLayoutResId() == R.layout.item_category_parent) {
            //group node
            LocatListBean parentInfo = (LocatListBean) node.getData();
            InventoryLocationAdapter.GroupViewHolder gvh = (InventoryLocationAdapter.GroupViewHolder) viewHolder;
            gvh.tvParentName.setText(parentInfo.getStorehouseLocatName());
            int childCount = node.getChildrenCount();
            setViewHolder(gvh.tvParentName, gvh.ivParent, node, viewHolder, childCount);
        } else if (node.getLayoutResId() == R.layout.item_category_children) {
            //child node
            LocatListBean info = (LocatListBean) node.getData();
            InventoryLocationAdapter.ContactViewHolder cvh = (InventoryLocationAdapter.ContactViewHolder) viewHolder;
            cvh.tvName.setText(info.getStorehouseLocatName());
//            cvh.ivParent.setPadding(SizeUtils.dp2px(20 * (info.getLevel() == null ? 0 : info.getLevel())), 0, 0, 0);
            int childCount = info.getChildList() == null ? 0 : info.getChildList().size();
            setViewHolder(cvh.tvName, cvh.ivParent, node, viewHolder, childCount);
        }
    }

    private void setViewHolder(TextView textView, ImageView imageView, ListTree.TreeNode node,
                               InventoryLocationAdapter.BaseViewHolder viewHolder, int childCount) {
        if (childCount == 0) {
            imageView.setVisibility(View.INVISIBLE);
        } else {
            imageView.setVisibility(View.VISIBLE);
        }
        if (node.isExpand()) {
            imageView.setImageResource(R.drawable.ic_zhankai);
        } else {
            imageView.setImageResource(R.drawable.ic_collapse);
        }
        textView.setOnClickListener(v -> clickItem(node));
        imageView.setOnClickListener(v -> expandItem(viewHolder));
    }

    private void expandItem(InventoryLocationAdapter.BaseViewHolder viewHolder) {
        int planePos = viewHolder.getLayoutPosition();
        ListTree.TreeNode treeNode = tree.getNodeByPlaneIndex(planePos);
        int nodePlaneIndex = tree.getNodePlaneIndex(treeNode);
        if (treeNode.isExpand()) {
            //收起
            int count = tree.collapseNode(nodePlaneIndex);
            notifyItemChanged(nodePlaneIndex);
            //通知view将相关的行删掉
            notifyItemRangeRemoved(nodePlaneIndex + 1, count);
        } else {
            //展开
            int count = tree.expandNode(nodePlaneIndex);
            notifyItemChanged(nodePlaneIndex);
            //通知view插入相关的行
            notifyItemRangeInserted(nodePlaneIndex + 1, count);
        }
    }

    private void clickItem(ListTree.TreeNode node1) {
        ListTree.TreeNode parent = node1.getParent();
        String title;
        String pIdPath;
        LocatListBean locatListBean;
        if (parent != null) {
            ListTree.TreeNode parent1 = parent.getParent();
            if (parent1 != null) {
                Object data1 = parent1.getData();
                LocatListBean parentData = (LocatListBean) parent.getData();
                //三级索引
                LocatListBean data = (LocatListBean) node1.getData();
                if (data1 instanceof LocatListBean) {
                    title = ((LocatListBean) data1).getStorehouseLocatName() + "-" + parentData.getStorehouseLocatName() + "-" + data.getStorehouseLocatName();
                    pIdPath = ((LocatListBean) data1).getStorehouseLocatId() + "," + parentData.getStorehouseLocatId() + "," + data.getStorehouseLocatId();
                } else {
                    title = parentData.getStorehouseLocatName() + "-" + data.getStorehouseLocatName();
                    pIdPath = parentData.getStorehouseLocatId() + "," + data.getStorehouseLocatId();
                }
                locatListBean = data;
            } else {
                //二级索引
                LocatListBean data = (LocatListBean) node1.getData();
                if (parent.getData() instanceof LocatListBean) {
                    LocatListBean info = (LocatListBean) parent.getData();
                    title = info.getStorehouseLocatName() + "-" + data.getStorehouseLocatName();
                    pIdPath = info.getStorehouseLocatId() + "," + data.getStorehouseLocatId();
                } else {
                    title = data.getStorehouseLocatName();
                    pIdPath = String.valueOf(data.getStorehouseLocatId());
                }
                locatListBean = data;
            }
        } else {
            //一级索引
            LocatListBean data = (LocatListBean) node1.getData();
            title = data.getStorehouseLocatName();
            pIdPath = String.valueOf(data.getStorehouseLocatId());
            locatListBean = data;
        }
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(locatListBean, title, pIdPath);
        }
    }

    protected class BaseViewHolder extends ListTreeAdapter.ListTreeViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }
    }

    private class GroupViewHolder extends InventoryLocationAdapter.BaseViewHolder {
        TextView tvParentName;
        ImageView ivParent;

        public GroupViewHolder(View itemView) {
            super(itemView);
            tvParentName = itemView.findViewById(R.id.tv_name);
            ivParent = itemView.findViewById(R.id.iv_name);
        }
    }

    private class ContactViewHolder extends InventoryLocationAdapter.BaseViewHolder {
        TextView tvName;
        ImageView ivParent;

        public ContactViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            ivParent = itemView.findViewById(R.id.iv_name);
        }
    }
}